import React, { useRef } from 'react';
import { Line } from '@ant-design/charts';
import { useState } from 'react';

const G2 = () => {
  const [ data, setData ] = useState([
    {
      year: '1991',
      value: 3,
    },
    {
      year: '1992',
      value: 4,
    },
    {
      year: '1993',
      value: 3.5,
    },
    {
      year: '1994',
      value: 5,
    },
    {
      year: '1995',
      value: 4.9,
    },
  ])
  const changeData = () => {
    setData([
      {
        year: '1991',
        value: 1,
      },
      {
        year: '1992',
        value: 7,
      },
      {
        year: '1993',
        value: 2,
      },
      {
        year: '1994',
        value: 3,
      },
      {
        year: '1995',
        value: 5,
      },
    ])
  }
  const config = {
    data,
    height: 300,
    xField: 'year',
    yField: 'value',
    point: {
      size: 5,
      shape: 'diamond',
      style: {
        fill: 'white',
        stroke: '#2593fc',
        lineWidth: 2,
      },
    },
  };
  const ref = useRef();

  // 导出图片
  const downloadImage = () => {
    ref.current.downloadImage();
  };

  // 获取图表 base64 数据
  const toDataURL = () => {
    console.log(ref.current.toDataURL());
  };

  return (
    <div>
      <button type="button" onClick={downloadImage} style={{ marginRight: 24 }}>
        导出图片
      </button>
      <button type="button" onClick={toDataURL}>
        获取图表信息
      </button>
      <button onClick = { changeData }>修改数据</button>
      <Line
        {...config}
        onReady={(plot) => {
          ref.current = plot;
        }}
      />
    </div>
  );
};

export default G2;